package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qianniu.napi.admin.entity.Address;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.service.IAddressService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收货地址
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-11
 */
@RestController
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private IAddressService addressService;

    // ************* bellow is mechant interface *********

    @RequestMapping("/api/user/address/list")
    @ResponseBody
    public Jinfo addrList(HttpServletRequest req) {
        Jinfo json = new Jinfo();
        if(ObjectUtil.getUid()==null){
            logger.debug("========not login");
            return json;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uid", req.getSession().getAttribute("uid"));
        List<Address> list = addressService.selectByMap(map);
        json.setInfo(list);
        return json;
    }

    @RequestMapping("/api/user/address/add")
    @ResponseBody
    public JsonDO addrAdd(@Valid Address bean,
                          HttpServletRequest req,
                          BindingResult bindingResult) {
        JsonDO json = new JsonDO();
        logger.debug("========address=" + JSON.toJSONString(bean));
        if (bean.getIsDefault() == 1) {
            Address addr = new Address();
            addr.setIsDefault(-1);
            addressService.update(addr, new EntityWrapper<Address>().eq("uid", ObjectUtil.getUid()).eq("mid", ObjectUtil.getMid()));
        }

        bean.setCreatetime(new Date());
        bean.setUid(ObjectUtil.getUid());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setState(1);// 默认启用
        bean.insert();
        return json;
    }

    @RequestMapping("/api/user/address/modify")
    @ResponseBody
    public JsonDO addrUpdate(@Valid Address bean,
                             HttpServletRequest req,
                             BindingResult br) {
        JsonDO json = new JsonDO();
        bean.setModifytime(new Date());
        bean.updateById();
        return json;
    }

    @RequestMapping("/api/user/address/detail")
    @ResponseBody
    public JsonDO getAddressDetailById(@Param("id") long id
    ) {
        JsonDO json = new JsonDO();
        Address bean = addressService.selectById(id);
        json.setData(bean);
        return json;
    }

    @RequestMapping("/api/user/address/del")
    @ResponseBody
    public Jinfo addrDel(@Param("id") long id) {
        Jinfo json = new Jinfo();
        addressService.deleteById(id);
        return json;
    }

    @RequestMapping("/api/user/address/default")
    @ResponseBody
    public JsonDO getDefaultAddressById() {
        JsonDO json = new JsonDO();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uid", ObjectUtil.getUid());
        map.put("is_default", 1);

        List<Address> bean = addressService.selectByMap(map);

        if (bean != null && bean.size() > 0) json.setData(bean.get(0));
        else json.setCode(4);
        return json;
    }


}
